package com.shijianchen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shijianchen.mapper.SpOtherAdminMapper;
import com.shijianchen.mapper.SpPlatformWrapper;
import com.shijianchen.projo.SpOtherAdmin;
import com.shijianchen.projo.SpPlatform;
import com.shijianchen.projo.query.GetUserQuery;
import com.shijianchen.projo.vos.UserItem;
import com.shijianchen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<SpOtherAdminMapper, SpOtherAdmin> implements UserService {
    @Autowired
    private SpPlatformWrapper spPlatformWrapper;
    /**
     * 登录相关业务处理
     * @param spOtherAdmin
     * @return spOtherAdminRet
     */
    @Override
    public SpOtherAdmin login(SpOtherAdmin spOtherAdmin) {
        LambdaQueryWrapper<SpOtherAdmin> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(SpOtherAdmin::getMobile,spOtherAdmin.getMobile());
        SpOtherAdmin spOtherAdminRet = this.getOne(userWrapper);
        if (spOtherAdminRet !=null) {
            String pwd = spOtherAdmin.getPassword();
            if(pwd.isEmpty()){
                return null;
            }
            String s = DigestUtils.md5DigestAsHex(pwd.getBytes());
            if(s.equals(spOtherAdminRet.getPassword())){
                return spOtherAdminRet;
            }
        }
        return null;
    }

    @Override
    public List<SpPlatform> getPlatform() {
        LambdaQueryWrapper<SpPlatform> queryWrapper = new LambdaQueryWrapper<>();
        return spPlatformWrapper.selectList(queryWrapper);
    }

    @Override
    public Page<UserItem> getUserList(Page<UserItem> page, GetUserQuery userItem) {
        return baseMapper.selectUserList(page ,userItem);
    }
}
